package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.ShopDao;
import com.swt.domain.Shop;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class ShopDaoImpl implements ShopDao {
    @Resource
    private HibernateTemplate hibernateTemplate;

    @Override
    public boolean shopAdd(Shop shop,int clubClubId) {
        shop.setClubClubId(clubClubId);

        try {

            hibernateTemplate.save(shop);


        }catch (Exception e){
            return false;
        }

        return true;
    }

    @Override
    public List<Shop> shopQuery(int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Shop where clubClubId = ? ";
        List<Shop> shopList = session.createQuery(sql).setParameter(0,clubClubId).list();


        return shopList;
    }

    @Override
    public List<Shop> shopQuery2(int id,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Shop where id = ? and clubClubId = ?";
        Query query = session.createQuery(sql);
        List<Shop> shopList2 = query.setParameter(0,id).setParameter(1,clubClubId).list();

        return shopList2;
    }
    @Override
    public boolean shopDelete(int id) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Shop shop = session.get(Shop.class,id);

        try {
            session.delete(shop);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public HibernateTemplate getHibernateTemplate() {

        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}
